package it.unibz.easytravel.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class PersonDbAdapter {

	private static final String TAG = "PersonDb";
	
	private static final String TABLE_NAME = "person";
	public static final String PERSON_ID = "_id";
	public static final String NAME = "name";
	public static final String PHONE_NUMBER = "phoneNumber";

	
	private DatabaseHelper mDbHelper;

	private static SQLiteDatabase mDb;

	private final Context mCtx;

	/**
	 * Database creation sql statement
	 */
	private static final String DATABASE_CREATE = 
		"create table " + TABLE_NAME + " ("
			+ PERSON_ID + " integer primary key autoincrement, "
			+ PHONE_NUMBER + " text,"
			+ NAME +" text);";

	public static void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE);
	}

	public static void onUpgrade(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	public PersonDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public PersonDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public void delete() {
		Log.i(TAG, "deleteTable");
		mDb.execSQL("DELETE FROM " + TABLE_NAME);
	}

	public static long insertPerson(String name, String phoneNumber) {
		Log.i(TAG, "insertPerson");
		ContentValues values = new ContentValues();
		values.put(NAME, name);
		values.put(PHONE_NUMBER, phoneNumber);
		return mDb.insert(TABLE_NAME, null, values);
	}

	/**
     * SQL = SELECT name FROM person WHERE phoneNumber=phoneNumber
     * 
     * @return name
     */
    public String getName(String phoneNumber) {
            Log.i(TAG, "getName");
            String where = PHONE_NUMBER+"='" + phoneNumber+"'";
            Cursor cursor = mDb.query(TABLE_NAME, new String[] { NAME }, where,
                            null, null, null, null);
            if (!cursor.moveToFirst())
                    throw new RuntimeException("user does not exist (PersonDbAdapter.getName(phoneNumber))");
            return cursor.getString(0);
    }
    
    
    /**
     * SQL = SELECT name FROM person WHERE _id=id
     * 
     * @return name
     */
    public String getName(int person_id) {
            Log.i(TAG, "getName");
            String where = PERSON_ID +"='" + person_id+"'";
            Cursor cursor = mDb.query(TABLE_NAME, new String[] { NAME }, where,
                            null, null, null, null);
            if (!cursor.moveToFirst())
                    throw new RuntimeException("user does not exist (PersonDbAdapter.getName(person_id))");
            return cursor.getString(0);
    }


	
	/**
	 * SQL = SELECT * FROM person WHERE person_id = person_id
	 * 
	 * @return cursor to result list
	 */
	public Cursor getPersonInfo(int person_id) {
		Log.i(TAG, "getPersonInfo");
		String where = PERSON_ID +"='" + person_id+"'";
		return mDb.query(TABLE_NAME, new String[] { PERSON_ID, NAME, PHONE_NUMBER }, where, null, null, null, null);
	}

	
	public static boolean personExists(String phoneNumber) {
		Log.i(TAG, "personExists");
		String where = PHONE_NUMBER+"='" + phoneNumber+"'";
        Cursor cursor = mDb.query(TABLE_NAME, new String[] { PERSON_ID }, where,
                        null, null, null, null);
        if (cursor.moveToFirst()) {return true;}
        else {return false;}
	}

	/**
     * SQL = SELECT _id FROM person WHERE phoneNumber=phoneNumber
     * 
     * @return _id
     */
	public static long getPersonId(String phoneNumber) {
		Log.i(TAG, "getPersonId");
        String where = PHONE_NUMBER+"='" + phoneNumber+"'";
        Cursor cursor = mDb.query(TABLE_NAME, new String[] { PERSON_ID }, where,
                        null, null, null, null);
        if (!cursor.moveToFirst())
                throw new RuntimeException("user does not exist (PersonDbAdapter.getPersonId(phoneNumber))");
        return cursor.getLong(0);
	}
}
